package com.drinks.yiniang.ui.activity.user.achievement;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.user.achievement.AchievementAdapter;
import com.drinks.yiniang.common.recycler.decoration.AchievementItemDecoration;
import com.drinks.yiniang.entity.user.achievement.AchievementJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.utils.GlideUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.widget.image.CircleImageView;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.RecyclerUtils;
import com.hansen.library.Constants;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.pickerimage.activity.PicturePreviewActivity;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的成就页面
 *
 * @author han
 * @date 2019-11-01
 */
public class AchievementActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        BaseQuickAdapter.OnItemClickListener {

    //nav
    private NavigationBarLayout nav_bar_achievement;
    //列表
    private BaseRecyclerView rcv_achievement;
    //header view
    //头像
    private CircleImageView iv_achievement_avatar;
    //昵称
    private TextView tv_achievement_nickname;
    //等级
    private TextView tv_achievement_level;

    private AchievementAdapter mAchievementAdapter;

    @Override
    protected void initView() {
        nav_bar_achievement = findViewById(R.id.nav_bar_achievement);

        rcv_achievement = findViewById(R.id.rcv_achievement);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        initAdapter();
        getMyAchievement();
    }

    private void initAdapter() {
        rcv_achievement.setLayoutManager(RecyclerUtils.getVerticalGridLayout(mContext, 3));
        rcv_achievement.addItemDecoration(AchievementItemDecoration.getItemDPDecoration(10));

        mAchievementAdapter = new AchievementAdapter(mContext, new ArrayList<AchievementJson.AchievementList>());
        mAchievementAdapter.addHeaderView(getHeaderView());
        mAchievementAdapter.setHeaderAndEmpty(true);
        mAchievementAdapter.setCommonEmpty(getLayoutInflater(), rcv_achievement);
        mAchievementAdapter.bindToRecyclerView(rcv_achievement);
    }

    //获取header
    private View getHeaderView() {
        View mHeaderView = getLayoutInflater().inflate(R.layout.header_achievement, (ViewGroup) rcv_achievement.getParent(), false);

        iv_achievement_avatar = mHeaderView.findViewById(R.id.iv_achievement_avatar);

        tv_achievement_nickname = mHeaderView.findViewById(R.id.tv_achievement_nickname);
        tv_achievement_level = mHeaderView.findViewById(R.id.tv_achievement_level);

        return mHeaderView;
    }

    @Override
    protected void initListener() {
        nav_bar_achievement.setOnNavgationBarClickListener(this);

        mAchievementAdapter.setOnItemClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_achievement;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        List<String> urlList = new ArrayList<>();

        for(int i = 0;i < mAchievementAdapter.getData().size(); i++) {
            urlList.add(mAchievementAdapter.getItem(position).getIcon());
        }

        Intent mIntent = new Intent(mContext, PicturePreviewActivity.class);
        mIntent.putExtra(Constants.KeyPos, position);

        mIntent.putStringArrayListExtra(Constants.KeyUrl, (ArrayList<String>) urlList);

        startActivity(mIntent);
    }


    //获取我的成就
    private void getMyAchievement() {
        showLoadingDialog();

        UserApiIO.getInstance().getMyArchievement(new APIRequestCallback<AchievementJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(AchievementJson res) {
                if(mAchievementAdapter == null) {
                    return;
                }

                setData(res.getData());

                mAchievementAdapter.getData().clear();
                mAchievementAdapter.addData(res.getData().getList());

                mAchievementAdapter.loadMoreEnd();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mAchievementAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void setData(AchievementJson.AchievementData data) {
        GlideUtils.loadAvatar(mContext, iv_achievement_avatar, data.getAvatar());
        tv_achievement_nickname.setText(data.getUsername());
        tv_achievement_level.setText("LV." + StringUtils.getNullEmptyConvertZero(data.getLevel()));
    }
}
